package cn.ecut.servlet;

import cn.ecut.model.User;
import cn.ecut.services.Userservices;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Objects;
@WebServlet("user/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        String password=req.getParameter("password");
        String msg="";
        User user=new User();
        user.setId(id);
        user.setPassword(password);

        Userservices userserices=new Userservices();
        user=userserices.CheckLogin(user);
        if(Objects.nonNull((user))){
            if(user.getPassword().equals(password)){
                HttpSession session=req.getSession();
                session.setAttribute("user",user);
                resp.sendRedirect("/homepage.jsp");
            }
            else{
                msg="用户名或密码错误！";
            }
        }
        else{
                msg="用户名不存在！";
        }
        req.setAttribute("msg",msg);
        if(msg!="")
            req.getRequestDispatcher("/login.jsp").forward(req,resp);

    }
}
